import json
import os
from PyQt5.QtWidgets import (
    QHBoxLayout,
    QVBoxLayout,
    QLabel,
    QStyledItemDelegate,
    QGridLayout,
    QPushButton,
    QFileDialog,
    QComboBox,
    QWidget,
    QGroupBox,
    QAbstractItemView,
    QTableWidgetItem,
    QTableWidget,
    QSpacerItem,
    QSizePolicy,
    QRadioButton,
)
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QObject, QEvent, QTimer
from PyQt5.QtGui import QPainter, QPen, QColor, QDoubleValidator
from ..dlg_model_params import DlgModelParams
from ..utils import Utils
import gui.controls.envs as envs
import copy
from enum import Enum
from .. import message_box as MBox


class DlgThemeSwitch(DlgModelParams):
    themeSwitch_save_signal = pyqtSignal(object)

    def __init__(self, parent=None):
        super(DlgThemeSwitch, self).__init__(parent)

        self.setWindowTitle("主题")
        self.pBtnOK.setFocusPolicy(Qt.NoFocus)
        self.pBtnCancel.setFocusPolicy(Qt.NoFocus)

        layout = QVBoxLayout()
        # 创建三个单选按钮
        self.option1 = QRadioButton("深色主题")
        self.option2 = QRadioButton("浅色主题")

        # # 默认选中第一个选项
        # self.option1.setChecked(True)

        # 将单选按钮添加到布局
        layout.addWidget(self.option1)
        layout.addWidget(self.option2)

        self.vbox.insertLayout(0, layout)

    def init(self, data):
        theme = data.get("theme")
        if theme == "dark":
            self.option1.setChecked(True)
        elif theme == "light":
            self.option2.setChecked(True)

    @pyqtSlot()
    def on_pBtnOK_clicked(self):

        if self.option1.isChecked():
            res = {"theme": "dark"}
        elif self.option2.isChecked():
            res = {"theme": "light"}
        else:
            return MBox.warning("请选择主题！")
        self.close()
        self.themeSwitch_save_signal.emit(res)

    def closeEvent(self, event):
        DlgThemeSwitch._shown = False  # 使用类名访问类属性
        DlgThemeSwitch._instance = None  # 清除实例引用
        event.accept()
